<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/6
 * Time: 14:14
 */
namespace App\Http\Controllers\admin_system;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Module\EntBookInfo;
use Storage;
use Cookie;
use DB;
use Validator;
use Redirect;
use Session;

class BookRecommendController extends Controller
{
    public function index($keyword = '')
    {
        if ($keyword != '') {
            $BookInfos = DB::table('VW_APP_BookInfo')->select('BookID','BookRecommend', 'TypeID', 'TypeName', 'BookTitle', 'IsShow', 'MasterID', 'ReleaseDate', 'RealName')
                ->where('DeleteMark', '1')
                ->where('BookTitle', 'like', '%' . $keyword . '%')
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        } else {
            $BookInfos = DB::table('VW_APP_BookInfo')->select('BookID','BookRecommend', 'TypeID', 'TypeName', 'BookTitle', 'IsShow', 'MasterID', 'ReleaseDate', 'RealName')
                ->where('DeleteMark', '1')
                ->orderBy('ReleaseDate', 'desc')
                ->paginate(20);
        }
        return view('admin_system.bookrecommend.index', [
            'BookInfo' => $BookInfos,
            'keyword' => $keyword
        ]);
    }

    public function indexupdate(Request $request)
    {
        $result = '';
        try {
            if ($request->ajax()) {
                $IDList = $request->json('IDList');
                if (is_numeric($IDList)) {
                    EntBookInfo::where('BookID', $IDList)->update([
                        'BookRecommend' => '1'
                    ]);
                    $result = 'ok';
                } else {
                    EntBookInfo::whereIn('BookID', explode(',', $IDList))->update([
                        'BookRecommend' => '1'
                    ]);
                    $result = 'ok';
                }
            }
        } catch (Exception $e) {
            $result = 'no';
        }
        return $result;
    }
}